from flask import Flask,render_template,request,redirect,url_for,flash
app = Flask(__name__)

@app.route("/")
@app.route("/index")
def hello_world():
    return "<h1>Hello Flask!</h1><img src=\"https://www.python.org/static/img/python-logo.png\">"


movieList = [
    {'title': 'My Neighbor Totoro', 'year': '1988'},
    {'title': 'Dead Poets Society', 'year': '1989'},
    {'title': 'A Perfect World', 'year': '1993'},
    {'title': 'Leon', 'year': '1994'},
    {'title': 'Mahjong', 'year': '1996'},
    {'title': 'Swallowtail Butterfly', 'year': '1996'},
    {'title': 'King of Comedy', 'year': '1999'},
    {'title': 'Devils on the Doorstep', 'year': '1999'},
    {'title': 'WALL-E', 'year': '2008'},
    {'title': 'The Pork of Music', 'year': '2012'}]


@app.route("/login",methods=["GET","POST"])
def login():
    if request.method == 'POST':  # 判断是否是 POST 请求
        title = request.form.get('title')  # 传入表单对应输入字段的 name 值
        year = request.form.get('year')
        print(title,year)
        movieList.append({'title': title, 'year': year})
        return redirect(url_for('movies'))
    return render_template('login.html')

@app.route("/add",methods=["GET"])
def add():
    if request.method == 'GET':  # 请求方式是get
        title = request.args.get('title')  # args取get方式参数
        year = request.args.get('year')
        print('add',title,year)
        movieList.append({'title': title, 'year': year})
        return redirect(url_for('movies'))
    return render_template('login.html')


@app.route("/movies")
def movies():
    return render_template('index.html',name="张三",movies=movieList)


# if __name__ == '__main__':
#     app.run(host='0.0.0.0', port=6000)




